//
//  blackJackSim.cpp
//  blackjackSim
//
//  Created by Luke Nomeland on 7/12/14.
//  Copyright (c) 2014 Luke Nomeland. All rights reserved.
//

#include "blackJackSim.h"

void blackJackSim::startGame(int money, int numPlayers, int numDecks)
{
    int deckArray [15];
    cutOff = (numDecks * 54) / 5;
    cardsRemaining = numDecks * 54;
    numOfDecks = numDecks;
    numOfPlayers = numPlayers;
    player playersArr [numOfPlayers];
    for (int i = 0; i < 15; ++i) {
        deckArray[i] = 4 * numDecks;
    }
    
    std::cout << "Enter bet amount: \n";
    std::cin >> bet;
    
    for (int i = 0; i < numOfPlayers; ++i) {
        playersArr[i].startingBankRoll = money;
        playersArr[i].highestBankRoll = money;
    }
    
    dealCards(deckArray, playersArr);
    
    std::cout << "Game over";

    
}

void blackJackSim::dealCards(int deckArr[], player playersArr[])
{
    bool cardLayed = false;
    int card;
    int player = 1;
    srand(time(NULL));
    while (playersArr[player].bankRoll > bet) {
        if (cutOff >= cardsRemaining)
        {
            reshuffleShoe(deckArr);
        }
            
        for (; cutOff < cardsRemaining; --cardsRemaining) {
            cardLayed = false;
            while (!cardLayed) {
                card = rand() % 11 + 1;
                if (deckArr[card] > 0) {
                    if (card == 1) {
                        playersArr[player].numOfAces++;
                        playersArr[player].cardSum += 11;
                    }
                    else {
                    playersArr[player].cardSum += card;
                    }
                    cardLayed = true;
                    if (playersArr[player].checkGame()) {
                        playersArr[player].bankRoll -= bet;
                    }
                    player %= numOfPlayers;
                    player += 1;
                    deckArr[card] = deckArr[card]--;
                }
            }

            
        }
    }
    
}

void blackJackSim::reshuffleShoe(int deckArray[])
{
    for (int i = 0; i < 15; ++i) {
        deckArray[i] = 4 * numOfDecks;
    }
    cardsRemaining = numOfDecks * 54;
}

